package com.ruoyi.cs.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.cs.domain.TUser;
import com.ruoyi.cs.domain.vo.UpdateUserVipDto;
import com.ruoyi.cs.mapper.TUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cs.mapper.TVipUserMapper;
import com.ruoyi.cs.domain.TVipUser;
import com.ruoyi.cs.service.ITVipUserService;

/**
 * 用户VIP变化记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-03-02
 */
@Service
@SuppressWarnings("all")
public class TVipUserServiceImpl implements ITVipUserService 
{
    @Autowired
    private TVipUserMapper tVipUserMapper;
    @Autowired
    private TUserMapper tUserMapper;

    /**
     * 查询用户VIP变化记录
     * 
     * @param id 用户VIP变化记录主键
     * @return 用户VIP变化记录
     */
    @Override
    public TVipUser selectTVipUserById(Long id)
    {
        return tVipUserMapper.selectTVipUserById(id);
    }

    /**
     * 查询用户VIP变化记录列表
     * 
     * @param tVipUser 用户VIP变化记录
     * @return 用户VIP变化记录
     */
    @Override
    public List<TVipUser> selectTVipUserList(TVipUser tVipUser)
    {
        return tVipUserMapper.selectTVipUserList(tVipUser);
    }

    /**
     * 新增用户VIP变化记录
     * 
     * @param tVipUser 用户VIP变化记录
     * @return 结果
     */
    @Override
    public int insertTVipUser(TVipUser tVipUser)
    {
        tVipUser.setCreateTime(DateUtils.getNowDate());
        return tVipUserMapper.insertTVipUser(tVipUser);
    }

    /**
     * 修改用户VIP变化记录
     * 
     * @param tVipUser 用户VIP变化记录
     * @return 结果
     */
    @Override
    public int updateTVipUser(TVipUser tVipUser)
    {
        return tVipUserMapper.updateTVipUser(tVipUser);
    }

    /**
     * 批量删除用户VIP变化记录
     * 
     * @param ids 需要删除的用户VIP变化记录主键
     * @return 结果
     */
    @Override
    public int deleteTVipUserByIds(Long[] ids)
    {
        return tVipUserMapper.deleteTVipUserByIds(ids);
    }

    /**
     * 删除用户VIP变化记录信息
     * 
     * @param id 用户VIP变化记录主键
     * @return 结果
     */
    @Override
    public int deleteTVipUserById(Long id)
    {
        return tVipUserMapper.deleteTVipUserById(id);
    }

    /**计算vip等级*/
    @Override
    public void vipStreamHandleV2(String userId, BigDecimal comsumeValue) {
        TUser tUser = tUserMapper.selectTUserById(userId);
        UpdateUserVipDto vo = new UpdateUserVipDto();
        vo.setUserId(userId);
        vo.setConsumeValue(comsumeValue);
        tVipUserMapper.updateUserVip(vo);
        /**判断用户VIP等级是否存在变化*/
        if(tUser.getUserVip() != vo.getUserVip()){
            TVipUser tVipUser = new TVipUser();
            tVipUser.setUserId(tUser.getUserId());
            tVipUser.setGoVip(tUser.getUserVip());
            tVipUser.setNowVip(vo.getUserVip());
            tVipUser.setVipStatus("0");
            tVipUser.setCreateTime(DateUtils.getNowDate());
            tVipUserMapper.insertTVipUser(tVipUser);
        }
    }
}
